package com.dcbeida.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.dcbeida.R;
import com.dcbeida.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 150410 on 2017/10/17.
 */

public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.StoreHolder> {
    private Activity mActivity;
    public List<String> mUris = new ArrayList<String>();
    private int defaultDrawable = R.drawable.songhuodan;//设置默认的选择图片
    public StoreAdapter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    public int getDefaultDrawable() {
        return defaultDrawable;
    }

    public void setDefaultDrawable(int defaultDrawable) {
        this.defaultDrawable = defaultDrawable;
    }

    @Override
    public StoreAdapter.StoreHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mActivity, R.layout.store_item, null);
        return new StoreHolder(view);
    }

    @Override
    public void onBindViewHolder(final StoreAdapter.StoreHolder holder, int position) {
        if (isShowAddItem(position)) {
            holder.imgFp.setImageResource(defaultDrawable);
            holder.imgDel.setVisibility(View.GONE);
        } else {

            holder.imgDel.setVisibility(View.VISIBLE);
            final String url = mUris.get(position).replaceAll("\\\\", "");
            Glide.with(mActivity).load(url).into(holder.imgFp);
        }

        holder.imgDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemDelClickListener != null) {
                    //注意这里使用getTag方法获取数据
                    mOnItemDelClickListener.onItemDelClick(v, holder.getLayoutPosition());
                }
            }
        });

        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取数据
                    mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        // 多返回一个用于展示添加图标
        if (mUris == null) {
            return 1;
        } else if (mUris.size() == Constants.MAX_IMAGE_SIZE) {
            return Constants.MAX_IMAGE_SIZE;
        } else {
            return mUris.size() + 1;
        }
    }


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

    }
    public interface OnRecyclerViewItemDelClickListener {
        void onItemDelClick(View view, int position);

    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    private OnRecyclerViewItemDelClickListener mOnItemDelClickListener = null;

    public void setOnItemDelClickListener(OnRecyclerViewItemDelClickListener listener) {
        this.mOnItemDelClickListener = listener;
    }


    static class StoreHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img_fp)
        ImageView imgFp;
        @Bind(R.id.img_del)
        ImageView imgDel;
        @Bind(R.id.rl_item)
        RelativeLayout rlItem;

        StoreHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private boolean isShowAddItem(int position) {
        int size = mUris == null ? 0 : mUris.size();
        return position == size;
    }

    public void setData(List<String> paths) {
        mUris = paths;
        notifyDataSetChanged();
    }

}
